/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once

#include <kmhdfs/client/block_reader.h>
#include <kmhdfs/common/checksum.h>
#include "DataTransferProtocol.h"
#include <memory>
#include <kmhdfs/network/buffered_socket_reader.h>
#include <kmhdfs/network/tcp_socket.h>
#include <kmhdfs/client/packet_header.h>
#include <kmhdfs/client/peer_cache.h>
#include <kmhdfs/server/datanode_info.h>
#include <kmhdfs/server/located_blocks.h>
#include <kmhdfs/common/session_config.h>

namespace Hdfs {
namespace Internal {

class RemoteBlockReader: public BlockReader {
public:
    RemoteBlockReader(const ExtendedBlock& eb, DatanodeInfo datanode,
                      PeerCache& peerCache, int64_t start, int64_t len,
                      const Token& token, const char* clientName, bool verify,
                      SessionConfig& conf);

    ~RemoteBlockReader();

    /**
     * Get how many bytes can be read without blocking.
     * @return The number of bytes can be read without blocking.
     */
    virtual int64_t available();

    /**
     * To read data from block.
     * @param buf the buffer used to filled.
     * @param size the number of bytes to be read.
     * @return return the number of bytes filled in the buffer,
     *  it may less than size. Return 0 if reach the end of block.
     */
    virtual int32_t read(char * buf, int32_t len);

    /**
     * Move the cursor forward len bytes.
     * @param len The number of bytes to skip.
     */
    virtual void skip(int64_t len);

private:
    bool readTrailingEmptyPacket();
    std::shared_ptr<PacketHeader> readPacketHeader();
    std::shared_ptr<Socket> getNextPeer(const DatanodeInfo& dn, const bool connectToDnViaHostname);
    void checkResponse();
    void readNextPacket();
    void sendStatus();
    void verifyChecksum(int chunks);

private:
    bool sentStatus;
    bool verify; //verify checksum or not.
    const ExtendedBlock & binfo;
    DatanodeInfo datanode;
    int checksumSize;
    int chunkSize;
    int connTimeout;
    int position; //point in buffer.
    int readTimeout;
    int size;  //data size in buffer.
    int writeTimeout;
    int64_t cursor; //point in block.
    int64_t endOffset; //offset in block requested to read to.
    int64_t lastSeqNo; //segno of the last chunk received
    PeerCache& peerCache;
    std::shared_ptr<BufferedSocketReader> in;
    std::shared_ptr<Checksum> checksum;
    std::shared_ptr<DataTransferProtocol> sender;
    std::shared_ptr<PacketHeader> lastHeader;
    std::shared_ptr<Socket> sock;
    std::vector<char> buffer;
};

}
}
